package com.ch.d0304;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@WebServlet(name = "OnlinesServlet", value = "/OnlinesServlet")

public class OnlinesServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
//        // 获取当前在线人数
//        int size = OnlineListener.onlineUsers.size();
//        PrintWriter out = response.getWriter();
//
//        out.printf("<dl><dt>在线人数:%d</dt>",size);
//        for (String onlineUser : OnlineListener.onlineUsers){
//            out.printf("<dd>%s></dd>", onlineUser);
//        }
//        out.printf("</dl>");

        Map<String, Date> onlineUsers = OnlineListener.onlineUsers;
        System.out.println(onlineUsers);
        String json = new Gson().toJson(onlineUsers);

        response.getWriter().printf(json);



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
